package com.linux.huhx.thread;

import java.util.concurrent.TimeUnit;

/**
 * Created by huhx on 2017-05-24.
 */
public class ThreadJoinTest2 {
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        Object object = new Object();
        synchronized (object) {
            while (myThread.flag) {
                System.out.println("before flag = " + myThread.flag);
                object.wait();
                System.out.println("after flag = " + myThread.flag);
            }
        }
        System.out.println("thread name " + Thread.currentThread().getName());
    }

    static class MyThread extends Thread {
        boolean flag = true;

        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("thread name " + Thread.currentThread().getName());
                flag = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //        public synchronized void joinTest() {
//            while (flag) {
//                try {
//                    wait();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
    }
}
